/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.projects.toolbox_basics_project.security.manager.impl;

import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;

import com.genia.toolbox.projects.toolbox_basics_project.bean.model.User;

/**
 * implementation of {@link UserDetails} for trampoline.
 */
@SuppressWarnings("serial")
public class TrampolineUserDetails
    implements UserDetails
{
  /**
   * the {@link User} to proxy.
   */
  private final User baseUser;

  /**
   * the {@link GrantedAuthority}s of the user.
   */
  private final GrantedAuthority[] grantedAuthorities;



  /**
   * constructor.
   * 
   * @param baseUser
   *          the {@link User} to proxy
   * @param grantedAuthorities
   *          the {@link GrantedAuthority}s of the user
   */
  public TrampolineUserDetails(User baseUser, GrantedAuthority[] grantedAuthorities)
  {
    this.baseUser = baseUser;
    this.grantedAuthorities = grantedAuthorities;
  }



  /**
   * Returns the authorities granted to the user. Cannot return
   * <code>null</code>.
   * 
   * @return the authorities (never <code>null</code>)
   * @see org.springframework.security.userdetails.UserDetails#getAuthorities()
   */
  public GrantedAuthority[] getAuthorities()
  {
    return grantedAuthorities;
  }



  /**
   * Returns the password used to authenticate the user. Cannot return
   * <code>null</code>.
   * 
   * @return the password (never <code>null</code>)
   * @see org.springframework.security.userdetails.UserDetails#getPassword()
   */
  public String getPassword()
  {
    return baseUser.getEncryptedPassword();
  }



  /**
   * Returns the username used to authenticate the user. Cannot return
   * <code>null</code>.
   * 
   * @return the username (never <code>null</code>)
   * @see org.springframework.security.userdetails.UserDetails#getUsername()
   */
  public String getUsername()
  {
    return baseUser.getLogin();
  }



  /**
   * Indicates whether the user's account has expired. An expired account cannot
   * be authenticated.
   * 
   * @return <code>true</code> if the user's account is valid (ie
   *         non-expired), <code>false</code> if no longer valid (ie expired)
   * @see org.springframework.security.userdetails.UserDetails#isAccountNonExpired()
   */
  public boolean isAccountNonExpired()
  {
    return baseUser.getAccountNonExpired();
  }



  /**
   * Indicates whether the user is locked or unlocked. A locked user cannot be
   * authenticated.
   * 
   * @return <code>true</code> if the user is not locked, <code>false</code>
   *         otherwise
   * @see org.springframework.security.userdetails.UserDetails#isAccountNonLocked()
   */
  public boolean isAccountNonLocked()
  {
    return baseUser.getAccountNonLocked();
  }



  /**
   * Indicates whether the user's credentials (password) has expired. Expired
   * credentials prevent authentication.
   * 
   * @return <code>true</code> if the user's credentials are valid (ie
   *         non-expired), <code>false</code> if no longer valid (ie expired)
   * @see org.springframework.security.userdetails.UserDetails#isCredentialsNonExpired()
   */
  public boolean isCredentialsNonExpired()
  {
    return baseUser.getCredentialsNonExpired();
  }



  /**
   * Indicates whether the user is enabled or disabled. A disabled user cannot
   * be authenticated.
   * 
   * @return <code>true</code> if the user is enabled, <code>false</code>
   *         otherwise
   * @see org.springframework.security.userdetails.UserDetails#isEnabled()
   */
  public boolean isEnabled()
  {
    return baseUser.getEnabled();
  }



  /**
   * getter for the baseUser property.
   * 
   * @return the baseUser
   */
  public User getBaseUser()
  {
    return baseUser;
  }

}
